<script setup>
import { ref } from "vue";
import { useUserStore } from "@/stores/useUserStore";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
const isRegister = ref(false);
const userStore = useUserStore();

const form = ref({
  student_id: "",
  password: "",
});

const rules = {
  student_id: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 5, max: 10, message: "用户名必须是 5-10位 的字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { pattern: /^\S{6,15}$/, message: "密码必须是 6-15位 的非空字符", trigger: "blur" },
  ],
};

const formRef = ref();
const router = useRouter();
const doLogin = async () => {
  try {
    await formRef.value.validate();
   await userStore.getUseInfo(form.value);
    ElMessage.success("登录成功");
    router.push("/");
  } catch (err) {
    console.log("登录失败", err);
    ElMessage.error("登录失败,请仔细检查输入!!!");
  }
};
</script>

<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">
      <!-- 注册相关表单 -->
      <el-form size="large" autocomplete="off" v-if="isRegister" :rules="rules" ref="formRef">
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="student_id">
          <el-input v-model="form.student_id" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input type="password" placeholder="请输入再次密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="button" type="primary" auto-insert-space> 注册 </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" @click="isRegister = false"> ← 返回 </el-link>
        </el-form-item>
      </el-form>

      <!-- 登录相关表单 -->
      <div v-else class="login-form">
        <el-form size="large" autocomplete="off" ref="formRef" :model="form" :rules="rules">
          <el-form-item>
            <h1 class="login-title">南青e站登录</h1>
          </el-form-item>
          <el-form-item prop="student_id">
            <el-input placeholder="请输入用户名" v-model="form.student_id"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" placeholder="请输入密码" v-model="form.password"></el-input>
          </el-form-item>
          <el-form-item class="flex">
            <div class="flex">
              <el-checkbox>记住我</el-checkbox>
              <el-link type="primary">忘记密码？</el-link>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button class="button" type="primary" auto-insert-space @click="doLogin"
              >登录</el-button
            >
          </el-form-item>
          <el-form-item class="flex">
            <el-link type="info" @click="isRegister = true"> 注册 → </el-link>
          </el-form-item>
        </el-form>
      </div>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background: url("../../assets/images/9.jpeg") no-repeat center;
  background-size: cover;
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    height: 400px;
    margin-top: 350px;
    padding: 30px;
    border-radius: 40px;
    user-select: none;
    background-color: rgba(255, 255, 255, 0.9);
    .title {
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
.login-title {
  font-size: 30px;
  transform: skewX(-20deg);
}
</style>
